
#include "DataGenerationThread.h"
#include "ChainWalkContext.h"
#include <time.h>
CDataGenerationThread::CDataGenerationThread(void)
{
	memset(zBuffer, 0x00, sizeof(zBuffer));
	bDataReady = 0;
	bShutdown = 0;
	m_nChainsCalculated = 0;
}

CDataGenerationThread::~CDataGenerationThread(void)
{
}

void CDataGenerationThread::threadProc()
{
#ifdef _WIN32
	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
#else
	nice(19);
#endif

	const DataGenerationThreadParameters *Parameters = (const DataGenerationThreadParameters *)Params;
	CChainWalkContext cwc;
	// Make a pointer to the beginning of the data block
	unsigned char *ptrCurrent = zBuffer;
	// Make a pointer to the end of the data block.
	unsigned char *ptrEnd = zBuffer + sizeof(zBuffer);
	while(bTerminateThreadFlag != 1)
	{
		cwc.GenerateRandomIndex();
//		uint64 nIndex = ;
		// Copy the content over
		memcpy(ptrCurrent, cwc.GetIndexPtr(), 8);
		// Increase the pointer location
		ptrCurrent += 8;
		int nPos;
		for (nPos = 0; nPos < Parameters->nRainbowChainLen - 1; nPos++)
		{
			cwc.IndexToPlain();
			cwc.PlainToHash();
			cwc.HashToIndex(nPos);
		}

//		nIndex = ;
		// Copy the content over
		memcpy(ptrCurrent, cwc.GetIndexPtr(), 8);
		// Increase the pointer location
		ptrCurrent += 8;
		m_nChainsCalculated++;
		if(ptrCurrent == ptrEnd)
		{
			// The old buffer isn't emptied yet. So we have to wait before writing our data
			while(bDataReady == 1 && bTerminateThreadFlag != 1)
			{
				Sleep(1000);
			}
			// Copy the data over
			memcpy(zDataChunk, zBuffer, sizeof(zDataChunk));
			// Mark the buffer as full
			bDataReady = 1;
			// Reset the data pointer
			ptrCurrent = zBuffer;

		}
	}
}
